package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.commons.utils.Result;
import com.bjpowernode.crm.settings.domain.DicType;
import com.bjpowernode.crm.settings.service.DicTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName: DictypeController<br/>
 * Package: com.bjpowernode.crm.settings.web.controller<br/>
 * Description: <br/>
 * Date: 2022/7/2 21:37<br/>
 * Auther:对的我写的，错的我同事写的<br/>
 */
@Controller
public class DictypeController {

    @Resource
    private DicTypeService dicTypeService;

    /**
     * 跳转至数据字典"首"页面
     * @return
     */
    @RequestMapping("/settings/dictionary/index.do")
    public String toShujuzidianPage()
    {
        return "settings/dictionary/index";
    }

    /**
     * 跳转至数据字典"类型" 页面
     * @return
     */
    @RequestMapping("/settings/dictionary/type/index.do")
    public String toDictypePage()
    {
        return "settings/dictionary/type/index";
    }

    /**
     * 跳转至数据字典"值" 页面
     * @return
     */
    @RequestMapping("/settings/dictionary/value/index.do")
    public String toDicvaluePage()
    {
        return "settings/dictionary/value/index";
    }

    /**
     * 查询全部数据字典类型
     * @return
     */
    @RequestMapping("/settings/dictionary/type/queryAllDictypes.do")
    @ResponseBody
    public Object queryAllDictypes()
    {
        return dicTypeService.queryAllDictypes();
    }

    /**
     * 跳转至添加字典值页面
     * @return
     */
    @RequestMapping("/settings/dictionary/type/toSavePage.do")
    public String toSavePage()
    {
        return "settings/dictionary/type/save";
    }

    /**
     * 判断编号是否重复
     * @param code
     * @return
     */
    @RequestMapping("/settings/dictionary/type/save/queryCode.do")
    @ResponseBody
    public Object queryCode(@RequestParam(value = "code")String code)
    {
        DicType dicType = dicTypeService.queryCode(code);
        if(null != dicType)
        {
            return Result.fail("密码重复!");
        }
        return Result.success();
    }

    /**
     * 添加数据字典值
     * @param dicType
     * @return
     */
    @RequestMapping("/settings/dictionary/saveCreateDicType.do")
    @ResponseBody
    public Object saveCreateDicType(DicType dicType)
    {
        try {
            Integer count = dicTypeService.saveCreateDicType(dicType);
            if(count == 1)
            {
                return Result.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("添加失败！");
        }
        return Result.fail("添加失败！");
    }

    /**
     * 携带数据跳转至编辑页面
     * @param request
     * @param code
     * @return
     */
    @RequestMapping("/settings/dictionary/type/toEditPage.do")
    public String toEditPage(HttpServletRequest request,@RequestParam(value = "code")String code)
    {
        DicType dicType = dicTypeService.queryCode(code);
        request.setAttribute("dicType",dicType);
        return "settings/dictionary/type/edit";
    }

    /**
     * 更新数据字典类型
     * @param dicType
     * @return
     */
    @RequestMapping("settings/dictionary/type/editDictype.do")
    @ResponseBody
    public Object editDictype(DicType dicType)
    {
        try {
            Integer count = dicTypeService.updateDictype(dicType);
            if(count != 1)
            return Result.fail("更新失败！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("更新异常，请稍后再试！");
        }
        return Result.success();
    }

    @RequestMapping("/settings/dictionary/type/delDictype.do")
     @ResponseBody
    public Object delDictype(@RequestParam(value = "code") String[] codes)
    {
        try {
            dicTypeService.deleteDictypebyCodes(codes);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("删除失败！");
        }
        return Result.success();
    }

}
